<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="resetForm('form')"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item
            label="所属校区"
            prop="deptId"
            v-if="form.type == 1 && maxPermis == 1"
          >
            <SchoolSelect
              v-if="dialogVisible"
              :defaultValue="form.deptId"
              @emit="setScool"
            />
          </el-form-item>
          <el-form-item label="培训层次" prop="levelId" v-if="form.type == 1">
            <TrainingSelect
              v-if="dialogVisible"
              :defaultValue="form.levelId"
              @e-trainData="setTrainId"
            />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="编码" prop="code">
            <el-input v-model.trim="form.code"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-radio-group v-model="form.status">
              <el-radio label="0">显示</el-radio>
              <el-radio label="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="排序" prop="orderBy">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { eduMajorsAdd, eduMajorsUpdate } from '@/api/admin/teach/eduMajors.js'
import Bus from '@/utils/bus.js'
export default {
  components: {
    SchoolSelect: () => import('@/components/dataSelect/schoolSelect'),
    TrainingSelect: () => import('@/components/trainingSelect'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        code: '',
        name: '',
        pid: '0',
        orderBy: '10',
        auditStatus: 2, //审核状态：1-待审核，2-审核通过，3-不通过
        status: '0', //状态;0-展示，1-不展示
        type: 1, //节点类型；1-专业，2-方向
        deptId: '',
        levelId: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
        deptId: [{ required: true, message: '请选择校区', trigger: 'change' }],
        levelId: [
          { required: true, message: '请选择培训层次', trigger: 'change' },
        ],
      },
    }
  },
  props: ['schoolId', 'maxPermis'],
  mounted() {
    Bus.$on('showDialogMajorAdd', (v) => {
      if (v) {
        if (v.type == 'add') {
          // 新增方向
          this.form.pid = v.row.id
          this.form.type = 2
          this.handleAddOrEdit(1)
        } else {
          //编辑
          this.checkedData = v.row
          this.handleAddOrEdit(2)
        }
      } else {
        // 新增专业
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogMajorAdd')
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
      } else {
        this.title = '新增'
        delete this.form.id
        if (!this.schoolId) {
          this.warnMsg('该账号未分配校区，请先配置校区')
          return
        }
        this.form.deptId = this.schoolId
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    submitForm(form) {
      // this.form.deptId = this.schoolId
      // console.log(this.form)
      // return
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      eduMajorsAdd(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshMajorList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //编辑
    edit() {
      eduMajorsUpdate(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshMajorList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    setScool(v) {
      this.form.deptId = v.id || ''
    },
    setTrainId(v) {
      this.form.levelId = v || ''
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.deptId = ''
      this.form.levelId = ''
      this.form.name = ''
      this.form.code = ''
      this.form.pid = '0'
      this.form.status = '0'
      this.form.type = 1
    },
  },
}
</script>